<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    //商品设置
    public function test(){
  /*  echo 11222;
    $uuid=md5(uniqid('user').time());
    db('clause')->insert(['content'=>$uuid]);*/
    //设定商品数量
    $count=30;
    $listKey="2019_04_15_goods_list";
    //创建连接redis对象
    $redis = new \Redis();
    $redis->connect('119.29.68.195', 6379);
    for($i=1;$i<=$count;$i++){
        //将商品id push到列表中
        $redis->rPush($listKey,$i);
    }
   /* $listKey="2019_04_15_goods_list";
      $redis = new \Redis();
    $redis->connect('119.29.68.195', 6379);
   $res= $redis->lrange($listKey,0,-1);
   print_r($res);die;*/
}
//开始秒杀
public function testkill(){
   /* echo 11222;
    $uuid=md5(uniqid('user').time());
    db('clause')->insert(['content'=>$uuid]);*/
    //假装是用户的唯一标识
   $uuid=md5(uniqid('user').time());
    //创建连接redis对象
    $redis = new \Redis();
    $redis->connect('119.29.68.195', 6379);
    $listKey="2019_04_15_goods_list";
    $orderKey="2019_04_15_buy_order";
    $failUserNum="2019_04_15_fail_user_num";
    if($goodsId=$redis->lPop($listKey)){
        //秒杀成功
        //将幸运用户存在集合中
        $redis->hSet($orderKey,$goodsId,$uuid);
    }else{
        //秒杀失败
        //将失败用户计数
        $redis->incr($failUserNum);
    }
    echo "SUCCESS";
}
//查询秒杀
public function test1(){
    $redis = new \Redis();
    $redis->connect('119.29.68.195', 6379);
    /*$redis->set('name','111');
     $res=$redis->get('name');*/
    //
    //$re= $redis->lrange('2019_04_15_goods_list',0,-1);
    //$re= $redis->get('2019_04_15_fail_user_num');
     $re= $redis->hGetAll('2019_04_15_buy_order');
 //   $redis->delete('2019_04_15_buy_order');
   print_r($re);die;
}
}
